package com.lvmama.vst.service;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lvmama.vst.dao.UserEntity;
import com.lvmama.vst.repository.UserRepository;

@Service
public class UserServiceImpl implements UserService {

	public static final String TUTORIAL_USER_SERVICE_KEY = "com.lvmama.vst.service.UserService_";
	
	@Autowired
	private UserRepository userRepository;
	
	@Override
	
	public Collection<UserEntity> getAllUser() {
		return this.userRepository.findAll();
	}

	@Override
	@Cacheable( value = "com.lvmama.vst.service.UserService", 
				cacheManager = "expired5MinutesCacheManager", 
				key = "#root.target.TUTORIAL_USER_SERVICE_KEY + #id")
	public UserEntity getUserById(String id) {
		return this.userRepository.getById(id);
	}

	@Override
	@Transactional
	public void addUser(UserEntity user) {
		this.userRepository.save(user);
	}

}
